import {
  HttpApi
} from '../../../model/index.js'
const httpApi = new HttpApi


Page({

  /**
   * 页面的初始数据
   */
  data: {
    // records:[],
    start: 1,
    size: 10,
    loading: false,
    records: [],
    activeTabIndex: '',
    showNullImg: false
  },

  makePhone() {
    wx.makePhoneCall({
      phoneNumber: '18296160359'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    var login = wx.getStorageSync('login')
   
    this.setData({
      dict: wx.getStorageSync("dict")
    })
    this.getData(1, this.data.size)
  },

  getData(_start, _size) {
    this.loading = true
    var _this = this
    var activeTabIndex = _this.data.activeTabIndex;
    var garbageTypeId = activeTabIndex;
    httpApi.garbageLog(_start, _size, garbageTypeId).then(res => {
      var arr1 = _this.data.records
      arr1 = [];
      var arr2 = res.data.records
      arr1 = arr1.concat(arr2)
      // console.log("orderList:",res.data)
      _this.setData({
        start: _start,
        records: arr1,
        loading: false
      })
      if (_this.data.records.length == 0) {
        _this.setData({
          showNullImg: true,
        })
      }
      // _this.setNewRecords()
    })
  },
  setNewRecords() {
    var app = getApp()
    var subDict = app.globalData.dict["投放状态"]
    console.log("subDict:", subDict)
    var newRecords = this.data.records
    for (var i = 0; i < newRecords.length; i++) {
      newRecords[i].btype = subDict[newRecords[i].btype]
    }
    console.log("new records:", newRecords)
    this.setData({
      records: newRecords
    })
  },
  onReachBottom() {
    console.log("reachbottom")
    // 下拉触底，先判断是否有请求正在进行中
    // 以及检查当前请求页数是不是小于数据总页数，如符合条件，则发送请求
    if (!this.loading) {
      this.getData(this.data.start + this.data.size, this.data.size)
    }
  },
  onHide() {
    clearInterval(this.data.time)
  },

  onUnload() {
    clearInterval(this.data.time)
  },
  tabChange(event) {
    let index = event.currentTarget.dataset.index;
    this.setData({ activeTabIndex: index });
    this.onLoad();
  }
})

